<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div class="nav-left" @mouseleave="handleMouseLeave">
        <h2 class="all" @mouseenter="isShowNav = true">全部商品分类</h2>
        <transition
          enter-active-class="animate__fadeIn"
          leave-active-class="animate__fadeOut"
        >
          <div class="sort animate__animated" v-show="isShowNav">
            <!-- 事件委派跳转路由 -->
            <div class="all-sort-list2" @click.prevent="toSearch">
              <!-- 一级分类 -->
              <div class="item" v-for="c1 in categoryList" :key="c1.categoryId">
                <h3>
                  <a :data-id="c1.categoryId" :data-level="1">{{
                    c1.categoryName
                  }}</a>
                  <!-- <router-link :to= '`/search?category1Id=${c1.categoryId}&categoryName=${c1.categoryName}`'>{{c1.categoryName}}</router-link> -->
                </h3>
                <div class="item-list clearfix">
                  <div class="subitem">
                    <dl
                      class="fore"
                      v-for="c2 in c1.categoryChild"
                      :key="c2.categoryId"
                    >
                      <!-- 二级分类 -->
                      <dt>
                        <a :data-id="c2.categoryId" :data-level="2">{{
                          c2.categoryName
                        }}</a>
                        <!-- <router-link :to= '`/search?category2Id=${c2.categoryId}&categoryName=${c2.categoryName}`'>{{c2.categoryName}}</router-link> -->
                      </dt>
                      <dd>
                        <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                          <a :data-id="c3.categoryId" :data-level="3">{{
                            c3.categoryName
                          }}</a>
                          <!-- <router-link :to= '`/search?category3Id=${c3.categoryId}&categoryName=${c3.categoryName}`'>{{c3.categoryName}}</router-link> -->
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
//引入辅助函数 mapState
import { mapState } from "vuex";
//引入过渡的样式库
import "animate.css";
export default {
  name: "Typenav",
  computed: {
    //计算属性 存储 categoryList
    ...mapState({
      categoryList: (state) => state.home.categoryList,
    }),
  },
  data() {
    return {
      isShowNav: false,//是否显示导航
    };
  },
  methods: {
    //点击一级、二级、三级分类导航的回调
    toSearch(event) {
      //获取点击分类的id
      const { id, level } = event.target.dataset;
      //获取点击分类的名字
      const { innerText } = event.target;
      //如果点击的不是导航就不跳转
      if (!level) return;
      //获取路径参数
      const {keyword} = this.$route.query
      //跳转search
      this.$router.push({
        path: "search",
        //传递query参数
        query: {
         
          ["category" + level + "Id"]: id, //几级导航:id
          categoryName: innerText, //点击的导航的文字
           keyword:keyword,//关键字
        },
      });
    },
    handleMouseLeave() { //处理鼠标移出
    //如果路径不是home 导航不展示
      if (this.$route.path !== "/home") {
        this.isShowNav = false;
      }
    },
  },
  mounted() {
    //组件一挂载分发getCategoryList给action,用于获取数据
    this.$store.dispatch("getCategoryList");
    //若路由是/home，则展示导航
    if (this.$route.path === "/home") {
      this.isShowNav = true;
    }
  },
};
</script>

<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;
      animation-duration: 0.5s;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;
            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &:hover {
            h3 {
              background-color: #e1251b;
              a {
                color: white;
                text-decoration: none;
                cursor: pointer;
              }
            }
            .fore {
              a:hover {
                color: #e1251b;
                font-size: bold;
                font-weight: 700;
                cursor: pointer;
              }
            }
            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}
</style>
